<?
require_once("header.misc.php");

class Advisory {
	
	public $info;
	
	function __construct($gid) {
		connectToDB();
		if(!is_numeric($gid)) {
			return false;
		}
		$this->info = advisoryInfo($gid);
		if(!$this->info) {
			echo "OH GOSH DARN!";
			return false;
		}
	}
	
	public function changeName($newname) {
		connectToDB();
		$awesome=mysql_query("UPDATE `advisory` SET `advisory_name`='" . mysql_real_escape_string($newname) . "' WHERE `advisory_id`='" . $this->info['advisory_id'] . "'");
		$this->info = advisoryInfo($this->info['advisory_id']);
		return true;
	}
	
	private function getRawLinker($uid) {
		connectToDB();
		$awesome=mysql_query("SELECT * FROM advisory_linker WHERE advisory_id='" . $this->info['advisory_id'] . "' AND people_id='" . mysql_real_escape_string($uid) . "'");
		return $awesome;
	}
	
	public function isInAdvisory($uid) {
		connectToDB();
		$awesome=$this->getRawLinker($uid);
		if(mysql_num_rows($awesome)>0) {
			return true;
		}
		return false;
	}
	
	public function isStudent($uid) {
		connectToDB();
		$awesome=$this->getRawLinker($uid);
		$awesome=mysql_fetch_array($awesome);
		$indeed=mysql_query("SELECT status.* FROM status WHERE status_id='" . mysql_real_escape_string($awesome['status_id']) . "' AND status_value='Student'");
		if(mysql_num_rows($indeed)>0) {
			return true;
		}
		return false;
	}
	
	public function isAdvisor($uid) {
		connectToDB();
		$awesome=$this->getRawLinker($uid);
		$awesome=mysql_fetch_array($awesome);
		$indeed=mysql_query("SELECT status.* FROM status WHERE status_id='" . mysql_real_escape_string($awesome['status_id']) . "' AND status_value='Advisor'");
		if(mysql_num_rows($indeed)>0) {
			return true;
		}
		return false;
	}
	
	public function studentList() {
		connectToDB();
		$awesome = mysql_query("SELECT people.* FROM people JOIN `advisory_linker` ON advisory_linker.people_id=people.people_id WHERE advisory_linker.advisory_id='" . $this->info['advisory_id'] . "'");
		$return=array();
		while($user=mysql_fetch_array($awesome)) {
			if(isStudent($user['people_id'])) {
				$return[$user['people_id']]=$user;
			}
		}
		return $return;
	}
	
	public function advisorList() {
		connectToDB();
		$awesome = mysql_query("SELECT people.* FROM people JOIN `advisory_linker` ON advisory_linker.people_id=people.people_id WHERE advisory_linker.advisory_id='" . $this->info['advisory_id'] . "'");
		$return=array();
		while($user=mysql_fetch_array($awesome)) {
			if(isAdvisor($user['people_id'])) {
				$return[$user['people_id']]=$user;
			}
		}
		return $return;
	}
	
	public function userList() {
		connectToDB();
		$awesome = mysql_query("SELECT people.* FROM people JOIN `advisory_linker` ON advisory_linker.people_id=people.people_id WHERE advisory_linker.advisory_id='" . $this->info['advisory_id'] . "'");
		$return=array();
		while($user=mysql_fetch_array($awesome)) {
			$return[$user['people_id']]=$user;
		}
		return $return;
	}
	
	public function addStudent($uid) {
		connectToDB();
		if(!isStudent($uid) OR $this->isInAdvisory($uid)) {
			return false;
		}
		mysql_query("INSERT INTO `advisory_linker` (`people_id`,`status_id`,`advisory_id`) VALUES ('" . mysql_real_escape_string($uid) . "','" . getStatusByName("Student") . "','" . $this->info['advisory_id'] . "')");
	}
	
	public function addAdvisor($uid) {
		connectToDB();
		if(!isAdvisor($uid) OR $this->isInAdvisory($uid)) {
			return false;
		}
		mysql_query("INSERT INTO `advisory_linker` (`people_id`,`status_id`,`advisory_id`) VALUES ('" . mysql_real_escape_string($uid) . "','" . getStatusByName("Advisor") . "','" . $this->info['advisory_id'] . "')");
	}
	
	public function removeMember($uid) {
		connectToDB();
		if(!$this->isInAdvisory($uid)) {
			return false;
		}
		mysql_query("DELETE FROM `advisory_linker` WHERE `people_id`='" . mysql_real_escape_string($uid) . "' LIMIT 1");
	}

}

